package org.darkforest.common.services.impl;

import org.darkforest.common.services.BundlingService;
import org.darkforest.dao.mapper.PBundlingGoodsMapper;
import org.darkforest.dao.mapper.PBundlingMapper;
import org.darkforest.model.PBundling;
import org.darkforest.model.PBundlingGoods;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by Administrator on 2016/1/13.
 */
@Service
@Transactional
public class BundlingServiceImpl implements BundlingService {

    @Autowired
    private PBundlingMapper pBundlingMapper;

    @Autowired
    private PBundlingGoodsMapper pBundlingGoodsMapper;

    @Override
    public int deleteByPrimaryKey(Integer blId) {
        return pBundlingMapper.deleteByPrimaryKey(blId);
    }

    @Override
    public int insert(PBundling record) {
        return pBundlingMapper.insert(record);
    }

    @Override
    public int insertSelective(PBundling record) {
        return pBundlingMapper.insertSelective(record);
    }

    @Override
    public PBundling selectByPrimaryKey(Integer blId) {
        return pBundlingMapper.selectByPrimaryKey(blId);
    }

    @Override
    public int updateByPrimaryKeySelective(PBundling record) {
        return pBundlingMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PBundling record) {
        return pBundlingMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据条件查询促销套餐
     *
     * @param pBundling
     * @return
     */
    @Override
    public List<PBundling> findListByCondition(PBundling pBundling) {
        return pBundlingMapper.findListByCondition(pBundling);
    }

    /**
     * 根据条件查询促销套餐包含套餐内的商品
     *
     * @param pBundling
     * @return
     */
    @Override
    public List<PBundling> findPBundlingListWithGoodsByCondition(PBundling pBundling) {

        List<PBundling> pBundlingList = pBundlingMapper.findPBundlingListWithGoodsByCondition(pBundling);

        List<PBundling> temp = new ArrayList<PBundling>();
        for (PBundling pBundling1 : pBundlingList) {
            StringBuilder sb = new StringBuilder();
            for (PBundlingGoods pBundlingGoods : pBundling1.getpBundlingGoodsList()) {
                sb.append(pBundlingGoods.getGoodsId() + ",");
            }
            pBundling1.setGoodsIds(sb.toString());

            if (pBundling.getGoodsId() != null && pBundling1.getGoodsIds().indexOf(pBundling.getGoodsId().toString()) == -1) {
                temp.add(pBundling1);
            }
        }
        pBundlingList.removeAll(temp);
        return pBundlingList;
    }

    /**
     * 根据套餐ID查询套餐详情以及套餐内的商品
     *
     * @param blId
     * @return
     */
    @Override
    public PBundling getBundlingWithGoodsById(Integer blId, Integer state) {
        Map map = new HashMap();
        map.put("blId", blId);
        map.put("state", state);
        return pBundlingMapper.getBundlingWithGoodsById(map);
    }

    /**
     * 新增优惠套餐活动包含优惠的商品
     *
     * @param pBundling
     * @return
     */
    @Override
    public AjaxReturnData addDiscountPackage(PBundling pBundling) throws Exception {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        try {
            pBundlingMapper.insertSelective(pBundling);
            for (PBundlingGoods pBundlingGoods : pBundling.getpBundlingGoodsList()) {
                pBundlingGoods.setBlId(pBundling.getBlId());
                pBundlingGoodsMapper.insert(pBundlingGoods);
            }

            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("新增优惠套餐成功");
            ajaxReturnData.setUrl("discountPackageList");
        } catch (Exception e) {
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("新增优惠套餐失败");
            throw new Exception("新增优惠套餐失败" + e);
        }

        return ajaxReturnData;
    }

    /**
     * 根据blId删除优惠套餐以及对应的商品
     *
     * @param ids
     * @param
     * @return
     */
    @Override
    public AjaxReturnData delBundlingBatch(Integer[] ids, String url) throws Exception {

        AjaxReturnData ajaxReturnData = new AjaxReturnData();

        try {
            List<Integer> blIds = new ArrayList<Integer>();
            for (Integer blId : ids) {
                blIds.add(blId);
            }
            pBundlingMapper.delBundlingBatch(blIds);
            pBundlingGoodsMapper.delBundlingGoodsBatch(blIds);
            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("删除成功");
            ajaxReturnData.setUrl(url);
        } catch (Exception e) {
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("删除失败，请联系管理员");
            throw new Exception("删除失败" + e);
        }


        return ajaxReturnData;
    }

    /**
     * 编辑后保存优惠套餐
     *
     * @param pBundling
     * @return
     */
    @Override
    public AjaxReturnData doModifyBundling(PBundling pBundling) throws Exception {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        try {
            pBundlingMapper.updateByPrimaryKeySelective(pBundling);
            for (PBundlingGoods pBundlingGoods : pBundling.getpBundlingGoodsList()) {
                pBundlingGoodsMapper.deleteByPrimaryKey(pBundlingGoods.getBlGoodsId());
                pBundlingGoods.setBlId(pBundling.getBlId());
                pBundlingGoodsMapper.insert(pBundlingGoods);
            }
            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("更新成功");
        } catch (Exception e) {
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("更新失败，请联系管理员");
            throw new Exception("更新失败" + e);
        }

        return ajaxReturnData;
    }

    /**
     * 根据优惠套餐ID查询 该套餐内详细的商品信息
     *
     * @param blId
     * @return
     */
    @Override
    public PBundling getBundlingWithGoodsDetailByBlId(Integer blId, Integer state) {
        Map map = new HashMap();
        map.put("blId", blId);
        map.put("state", state);
        return pBundlingMapper.getBundlingWithGoodsDetailByBlId(map);
    }

    /**
     * 修改优惠套餐活动状态
     *
     * @param blId
     * @param state
     * @param url
     * @return
     */
    @Override
    public AjaxReturnData updateBlState(Integer blId, Integer state, String url) throws Exception {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();

        try {
            PBundling pBundling = new PBundling();
            pBundling.setBlId(blId);
            pBundling.setBlState(state);
            pBundlingMapper.updateByPrimaryKeySelective(pBundling);
            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("修改优惠套餐活动状态成功");
            ajaxReturnData.setUrl(url);
        } catch (Exception e) {
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("修改优惠套餐活动状态失败");
            throw new Exception("修改优惠套餐活动状态失败" + e);
        }
        return ajaxReturnData;
    }
}
